package loginCET;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.example.cet.MainActivity;
import com.example.cet.R;

import android.R.integer;
import android.R.string;
import android.app.AlertDialog;
import android.content.*;
import android.os.Looper;
import android.widget.Toast;

public class Login {
	private static Login login = new Login();
	HttpResponse response;
	HttpEntity httpEntity;
	ContentValues result;
	private static ContentValues values = new ContentValues();
	public Login() {

	}

	public static Login getLogin() {
		return login;
	}

	private void parse(Document doc,ContentValues values) {
		Elements elements = doc.getElementsByTag("tr");
		for (Element elementtwo : elements) {

			if (elementtwo.getElementsByTag("th").text().equals("姓名：")) {
				values.put("name", elementtwo.getElementsByTag("td").text());
			} else if (elementtwo.getElementsByTag("th").text().equals("学校：")) {
				values.put("school", elementtwo.getElementsByTag("td").text());
			} else if (elementtwo.getElementsByTag("th").text().equals("考试类别：")) {
				values.put("type", elementtwo.getElementsByTag("td").text());
			} else if (elementtwo.getElementsByTag("th").text().equals("准考证号：")) {
				values.put("id", elementtwo.getElementsByTag("td").text());
			} else if (elementtwo.getElementsByTag("th").text().equals("考试时间：")) {
				values.put("time", elementtwo.getElementsByTag("td").text());
			} else if (elementtwo.getElementsByTag("th").text().equals("总分：")) {

				String result = elementtwo.getElementsByTag("td").text();
				int number;
				int start;
				String[] score = new String[4];

				for (int j = 0; j < 4; j++) {
					number = result.indexOf(' ');
					score[j] = result.substring(0, number);
					start = result.indexOf('：') + 1;
					result = result.substring(start);
				}
				values.put("sum", score[0]);
				values.put("listen", score[1]);
				values.put("read", score[2]);
				values.put("synthesize", score[3]);
				values.put("write", result);
			}
		}
	}

	private int parseHTML(Document doc) {
		Element content = doc.getElementById("c");
		Element element = content.getElementsByClass("error").first();

		if ((element != null)) {
			if (element.text().equals("姓名或考号输入有误")) {
				return 2;
			}
		} else{
			parse(doc,values);
		}
		return 0;
		

	}

	public static ContentValues getContentValues() {
		return values;
	}

	// 处理登录连接的函数，可以放在Login类里面
	public int loginChsi(String name, String number) {
		int myException=1;
		try {
			name = URLEncoder.encode(name, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		HttpClient httpClient = new DefaultHttpClient();
		HttpGet get = new HttpGet("http://www.chsi.com.cn/cet/query?zkzh="
				+ number + "&xm=" + name);
		get.setHeader("Referer", "http://www.chsi.com.cn/cet/");
		get.setHeader("Host", "www.chsi.com.cn");
		get.setHeader("DNT", "1");
		get.setHeader(
				"Cookie",
				"JSESSIONID=961364397AE6BAC015415BFC4FF4CE3E; __utma=65168252.724413170.1378560297.1378560297.1378741652.2; __utmb=65168252.2.10.1378741652; __utmz=65168252.1378741652.2.2.utmcsr=baidu.com|utmccn=(referral)|utmcmd=referral|utmcct=/s; __utmc=65168252");

		try {
			response = httpClient.execute(get);

			httpEntity = response.getEntity();
			String content = EntityUtils.toString(httpEntity);
			
			myException=parseHTML(Jsoup.parse(content));

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			myException = 0; // 网络故障
		}

		return myException;
	}

}
